const net = require('net');

const clients = [];
const server = net.createServer();

server.on('connection', socket => {
    clients.push(socket);

    socket.on('data', data => {
        clients.forEach(client => {
            if (client !== socket && client.readyState === 'open') {
                try {
                    client.write(data);
                } catch(err) {
                    // console.log(err);
                }
            }
        });
    });
});

server.on('close', socket => {
    const index = clients.indexOf(socket);
    if (index !== -1) {
        clients.splice(index, 1);
    }
});

server.listen(8888, '0.0.0.0');
